<?php
/**
 * Load
 * Inclusion of files made easy.
 *
 * @package Mundy
 */

namespace mundy;

spl_autoload_register('mundy\Load::auto');

class Load {

    public $mPath, $mPostfix,
           $vPath, $vPostfix,
           $cPath, $cPostfix;

    /**
     * @param array $vars
     */
    public function __construct( $vars ) {
        if( is_array($vars) ) {
            foreach( $vars as $k => $v ) {
                $this->$k = ( stristr($k, 'path') ) ? trim($v, '/\\').'/' : $v;
            }
        }
    }

    /**
     * Implements autoloading of classes.
     *
     * @param string $className
     */
    public static function auto( $className ) {
        $file = 'lib/'.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
        if( is_file($file) ) {
            include $file;
        }
        else var_dump('Shit! Class file not found: ', $file);
    }

    /**
     * Load a view file, passing vars.
     *
     * @param string $view The name of the view file
     * @param array $vars Associative array to pass vars for use in the view file
     */
    public function view($view, $vars = array()) {
        $file = $this->v_path . '/' . $view . '.php';
        extract($vars);

        ob_start();
        include $file;
        $out = ob_get_contents();
        ob_end_clean();

        echo $out;
    }
}